import {classicBeh} from '../classic-beh'

const mMgr = wx.getBackgroundAudioManager()

Component({
    behaviors: [classicBeh],
    properties: {
        src: String

    },
    data: {
        playing: false,
        pauseSrc: 'images/player@pause.png',
        playSrc: 'images/player@play.png'

    },

    lifetimes: {
        attached: function (event) {
            /*this._recoverPlaying()
            this._monitorSwitch()*/
            //只能在wx:if中
            this._recoverStatus()
            this._monitorSwitch()
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行

        },
    },


    methods: {

        onPlay: function (event) {
            if (!this.data.playing) {
                this.setData(
                    {
                        playing: true
                    }
                )
                mMgr.title = "南八"
                mMgr.src = this.properties.src
            } else {
                this.setData({
                    playing: false
                })
                mMgr.pause()
            }


        }
        ,
        _recoverStatus: function(event) {
            if (mMgr.paused) {
                this.setData({
                    playing: false
                })
                return
            }
            if (mMgr.src === this.properties.src) {
                this.setData({
                    playing: true
                })
            }
        }
        ,     //播放器检测,
        _monitorSwitch: function () {
            mMgr.onPlay(() => {
                this._recoverStatus()
            })
            mMgr.onPause(() => {
                this._recoverStatus()
            })
            mMgr.onStop(() => {
                this._recoverStatus()
            })
            mMgr.onEnded(() => {
                this._recoverStatus()
            })
        }


    }
})
